package com.orilife.trade;

import com.example.orllife.CoolMakingApplication;
import com.example.orllife.R;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MyOrderActivity extends FragmentActivity {
	Fragment myNewOrderFragment,myFinFragment,myChargFragment;
	RadioGroup orderMenuRb;
	Button backBt;
	RadioButton newOrderRb;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.myorderlayout);
		CoolMakingApplication.getInstance().addActivity(this);
		orderMenuRb = (RadioGroup)findViewById(R.id.orderradiogroup);
		newOrderRb =(RadioButton)findViewById(R.id.neworderrb);
		backBt= (Button)findViewById(R.id.backbt);
		backBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MyOrderActivity.this.finish();
			}
		});
		orderMenuRb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				int position=0;
				switch(checkedId){
				case R.id.neworderrb:position=0;break;
				case R.id.chargedrb:position=1;break;
				case R.id.finishedrb:position=2;break;
				}
				setSelet(position);
				
			}
		});
		
		setSelet(0);
		newOrderRb.setChecked(true);
		
	}
	
	private void setSelet(int i) {
		// TODO Auto-generated method stub
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		hideFragment(transaction);
		switch (i) {
		case 0:
			
			if (myNewOrderFragment == null) {
				myNewOrderFragment = new MyHaveNotChargedOrderFeagment();
				transaction.add(R.id.orderframelayout, myNewOrderFragment);
			} else {
				transaction.show(myNewOrderFragment);
			}
			break;
		case 1:
			if (myChargFragment == null) {
				myChargFragment = new MyAlreadyChargedFragment();
				transaction.add(R.id.orderframelayout, myChargFragment);
			} else {
				transaction.show(myChargFragment);
			}
		
			break;
			

		case 2:
			
			if (myFinFragment == null) {
				myFinFragment = new MyAlreadyFinishedFragment();
				transaction.add(R.id.orderframelayout, myFinFragment);
			} else {
				transaction.show(myFinFragment);
			}
			
			break;
		}

		transaction.commit();

	}
	

	private void hideFragment(FragmentTransaction transaction) {
		// TODO Auto-generated method stub
		if (myNewOrderFragment != null) {
			transaction.hide(myNewOrderFragment);
		}
		if (myFinFragment != null) {
			transaction.hide(myFinFragment);
		}
		if (myChargFragment != null) {
			transaction.hide(myChargFragment);
		}
		
	}
	
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		CoolMakingApplication.getInstance().removeActivity(this);
	}

}
